package com.wisemoresystem.seal.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class AuthService extends BaseService {

	public List<Map<String, Object>> selectSealAuthList(String srh_name,String today,Page page) {
		String sql = "select t._id,t.bank_id,t.begin_user_id,t.end_user_id,t.type,t.flag,t.user_id,tub.uname bname,tue.uname ename,tb.name kname,t.time_get "
				+ " ,t.time_back from t_seal_auth t "
				+ " left outer join t_user tub on t.begin_user_id=tub._id "
				+ " left outer join t_user tue on t.end_user_id=tue._id "
				+ " left outer join t_bank tb on t.bank_id=tb._id "
				+ " where (t.begin_user_id=? or t.end_user_id=?) and t.status=0 and t.bank_id=? and tub.uname like ? and t.flag='生效' and ?<=t.time_back "
				+ " order by t.created asc ";
		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentUserId());
		params.add(WebTools.getCurrentUserId());
		params.add(WebTools.getCurrentBankId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and tub.uname like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(today);
	
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(),page);
		return list;
	}
	
	public List<Map<String,Object>> selectUserRoleList(){
		
		String sql ="select t._id,t.uname,tr.name from t_user t "
				+ " left outer join t_role tr on t.role_id=tr._id "
				+ " where t.status=0 and t._id=? ";
		
		List<Map<String,Object>> list = jt.queryForList(sql,new String[]{WebTools.getCurrentUserId()});
		
		return list;
	}
	
	public List<Map<String,Object>> selectEmpower(){
		
		String sql ="select t.* from t_empower t where t.status=0 ";
		
		List<Map<String,Object>> list = jt.queryForList(sql);
		
		return list;
	}
	
	public List<Map<String,Object>> selectSealUserList(){
		
		String sql ="select t._id,t.uname from t_user t "
				+ " where t.status=0 and t.bank_id=? and t.user_level='非主管' and t.is_auth='是' and t._id not in "
				+ " (select ts.end_user_id from t_seal_auth ts where ts.begin_user_id=?  "
				+ " and ?<=ts.time_back and ts.flag='生效') and t._id!=? "
				+ " order by t.uname desc";
		
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = df.format(c.getTime());
		
		List<String> params = new ArrayList<String>();
		params.add(WebTools.getCurrentBankId());
		params.add(WebTools.getCurrentUserId());
		params.add(today);
		params.add(WebTools.getCurrentUserId());
	
		
		List<Map<String,Object>> list = jt.queryForList(sql,params.toArray());
		
		return list;
	}
	
	public List<String> selectNotAuth(){
		List<String> ulist = new ArrayList<String>();
		String sql ="select * from t_seal_auth ts "
				+ "where ?>ts.time_back and ts.flag='生效' ";
		
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = df.format(c.getTime());
		
		List<String> params = new ArrayList<String>();
		params.add(today);
		List<Map<String,Object>> list = jt.queryForList(sql,params.toArray());
		if(ChkTools.isNotNull(list)){
			for(Map<String,Object> users:list){
				ulist.add(users.get("end_user_id").toString());
			}
			
		}
		return ulist;
	}
	
	public boolean auth(String boeing){
		boolean flag = false;
		String sql = "select t.* from t_user t "
				+ " where t.status=0 and t.boeing=? and t.bank_id=? and t.user_level='三级主管' ";
		
		List<String> params = new ArrayList<String>();
		params.add(boeing);
		params.add(WebTools.getCurrentBankId());
		List<Map<String,Object>> list = jt.queryForList(sql,params.toArray());
		if(ChkTools.isNotNull(list)){
			flag = true;
		}
		
		return flag;
	}
}
